#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include "msgq.h"
#include "tools.h"

Ctos ctos;
Stoc stoc;

void destory_acc(void)
{
	char path[PATH_MAX];
	Account temp;
	sprintf(path,"%s/%u",ACC_PATH,ctos.acc.id);
	pf("acc path:%s\n",path);
	int fd = open(path,O_RDWR);
	if(ERROR == fd)
	{
		perror("open acc");
		strcpy(stoc.str,"未找到该用户，请确认账号");
	}
	else
	{
		read(fd,&temp,sizeof(Account));
		if(0 == strcmp(temp.name,ctos.acc.name) && 0 == strcmp(temp.card, ctos.acc.card) &&
temp.pass == ctos.acc.pass)
		{
			if(0 == temp.money)
			{
				close(fd);
				unlink(path);
				strcpy(stoc.str,"销户成功");
			}
			else
			{
				strcpy(stoc.str,"您的卡里还有余额，请取出后再销户");
			}
		}
		else
		{
			strcpy(stoc.str,"身份信息有误，请再次确认");
		}
	}
	stoc_send(&stoc);
	
}

int main()
{
	init_msgq();
	for(;;)
	{
		ctos_recv(&ctos,TYPE_DESTORY);
		stoc.type = ctos.pid;
		destory_acc();
	}
}
